// Copyright (C) 2011 YANG Hao (yangh2007@gmail.com). All rights reserved.
//
// This file is part of the (cute)octopus project.
//
// This file may be distributed and/or modified under the terms of the
// GNU General Public License version 2 as published by the Free Software
// Foundation.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Author(s): YANG Hao


#ifndef OTASK_H
#define OTASK_H

#include <QObject>
#include <QAction>
#include <QThread>
#include <QVariant>
#include <QCoreApplication>

#include "ostd.h"

class OAction;
class OEvent;
class ODoc;

DECLARE_PRIVATE(OHandlerThread)
class OCTOPUS_EXPORT OTask : public QObject {
	Q_OBJECT
public:
	typedef OStd::TaskRet (*Handler)(void);
	OTask(Handler h, bool async = false);
	// entry
	void start();
protected:
	virtual OStd::TaskRet exec();

signals:
	void finished();
	void terminated();

protected:
	OStd::TaskRet m_ret;
	Handler m_handler;
	QThread* m_asyncThread;

	ACCESS_PRIVATE(OHandlerThread)
};

template <typename _CLASS_> 
class OClassTask : public OTask {
public:
	typedef OStd::TaskRet (_CLASS_::*ClassHandler)(void);

	OClassTask(ClassHandler h, bool async = false)
		: OTask((OTask::Handler)h, async){}
	inline void setClass(_CLASS_* c){m_class = c;}

protected:
	virtual OStd::TaskRet exec(){return (m_class->*((ClassHandler)m_handler))();}
	_CLASS_* m_class;
};

#endif // OTASK_H
